package com.lyz.dataStructure.面试算法题.GrowingIO;

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/9/2 13:51
 *@Description:
 **/

/*
两条线程实现交替打印奇偶数
 */
public class ThreadExchange1 {
    private static volatile int i = 1;
    public static void main(String[] args) {
        new Thread(new PrintRunner(1),"ji shu").start();
        new Thread(new PrintRunner(0),"ou shu").start();

    }
    static class PrintRunner implements Runnable{
        private int result;
        public PrintRunner(int i) {result = i;}
        @Override
        public void run() {
            while(i < 10){
                // 据说用除余运算也一样，因为编译器优化过了
                if((i & 1) == result){
                    System.out.println(Thread.currentThread().getName() + i);
                    // 这里必须要在打印完后进行加操作
                    i++;
                }
            }
        }
    }
}

